package com.ndp.fb.walle.controller;

import com.ndp.fb.rdb.model.EcMaterielCategory;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.walle.business.EcMaterielCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hexin on 2016/5/24.
 */
@RestController
@RequestMapping("/ecMaterielCategory")
public class EcMaterielCategoryController {

    @Autowired
    EcMaterielCategoryService ecMaterielCategoryService;

    @RequestMapping("findAll")
    public List<EcMaterielCategory> findAll(HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        return ecMaterielCategoryService.findAll(user);
    }

    @RequestMapping("supportCreate")
    public List<EcMaterielCategory> supportCreate(HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        return ecMaterielCategoryService.findSupportCreate(user);
    }

    @RequestMapping("supportTemplateFilter")
    public List<EcMaterielCategory> supportTemplateFilter(HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        return ecMaterielCategoryService.findSupportTemplateFilter(user);
    }

    @RequestMapping("supportMaterielFilter")
    public List<EcMaterielCategory> supportMaterielFilter(HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        List<EcMaterielCategory> ecMaterielCategories = ecMaterielCategoryService.supportMaterielFilter(user);

        List<EcMaterielCategory> result = new ArrayList<>();
        for (EcMaterielCategory ecMaterielCategory : ecMaterielCategories) {
            String categoryType = ecMaterielCategory.getCategoryType();
            //创意
            if ("creativeType".equals(categoryType)){
                for (EcMaterielCategory e : ecMaterielCategories) {
                    if ("adCarousel".equals(e.getCategoryType())
                            || "adLink".equals(e.getCategoryType())){
                        ecMaterielCategory.getChildren().add(e);
                    }
                }
            }

            else if ("adCarousel".equals(categoryType)
                    || "adLink".equals(categoryType)){
                continue;
            }

            result.add(ecMaterielCategory);
        }

        return result;
    }
}
